🥅 Reject json0 ops that ot-json0 can only apply - #725
Draft
alecgibson wants to merge 1 commit into
Draft
Conversation
`ot-json0`'s `apply()` quietly treats an op that isn't a real array as a
no-op: it walks the op with `op.length` and numeric indexing, so a bare
component or a plain number iterates zero times and the snapshot comes
back untouched. `backend.submit()` has always accepted those, so they
commit, bump the version and get published, having changed nothing.
Its `compose()` and `invert()` are not so relaxed, though. Both assume a
real array, and throw on anything else:
```
compose({p: ['x'], oi: 1}, fixup) -> dest.push is not a function
compose({0: {…}, length: 1}, fixup) -> dest.push is not a function
compose([null], fixup) -> reading 'p' of null
invert({p: ['x'], oi: 1}) -> op.slice is not a function
```
`$fixup()` composes, and it's called from `apply` middleware, where a
throw is uncaught. So every one of those shapes is a remote crash on any
server that fixes ops up, needing nothing more than a raw socket frame —
no ShareDB client sends them, since `Doc._submit()` runs
`type.normalize()` first, but nothing stops a hand-written one.
This change checks the shape of a submitted op as well as its paths, in
`submit-request` rather than in `ot.apply()`. It has to happen before
the op reaches any type function, and it can't go in `checkOp()`, which
runs before we have the snapshot and so doesn't know the document's
type. Ops we've already committed are deliberately left alone, since
rejecting a historical no-op would make that document unreadable
through `fetchSnapshot()`.
This is an API change: a json0 op that isn't an array used to commit as
a silent no-op, and is now rejected with `ERR_OT_OP_BADLY_FORMED`. That
seems worth it, since the behaviour being removed is a crash for fixup
users and a no-op for everyone else — but it is why three of our own
tests in `test/backend.js` needed their ops wrapping, which is a fair
signal that the shape is easy to write by accident. Clients are
unaffected.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ot-json0'sapply()quietly treats an op that isn't a real array as a no-op: it walks the op withop.lengthand numeric indexing, so a bare component or a plain number iterates zero times and the snapshot comes back untouched.backend.submit()has always accepted those, so they commit, bump the version and get published, having changed nothing.Its
compose()andinvert()are not so relaxed, though. Both assume a real array, and throw on anything else:$fixup()composes, and it's called fromapplymiddleware, where a throw is uncaught. So every one of those shapes is a remote crash on any server that fixes ops up, needing nothing more than a raw socket frame — no ShareDB client sends them, sinceDoc._submit()runstype.normalize()first, but nothing stops a hand-written one.This change checks the shape of a submitted op as well as its paths, in
submit-requestrather than inot.apply(). It has to happen before the op reaches any type function, and it can't go incheckOp(), which runs before we have the snapshot and so doesn't know the document's type. Ops we've already committed are deliberately left alone, since rejecting a historical no-op would make that document unreadable throughfetchSnapshot().This is an API change: a json0 op that isn't an array used to commit as a silent no-op, and is now rejected with
ERR_OT_OP_BADLY_FORMED. That seems worth it, since the behaviour being removed is a crash for fixup users and a no-op for everyone else — but it is why three of our own tests intest/backend.jsneeded their ops wrapping, which is a fair signal that the shape is easy to write by accident. Clients are unaffected.🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com